package com.qianbao.service.order.impl;

import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.components.uuid.SVUuidTool;
import com.qianbao.dao.order.OrderDao;
import com.qianbao.domain.order.OrderEntity;
import com.qianbao.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao dao;

    @Override
    public List<OrderEntity> findOrderWithOrderStateCustomId(String orderState, String customId) throws CommonException {

        if (orderState == null || orderState.length() == 0 || customId == null || customId.length() == 0){
            throw new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        if (orderState.equals("-1")){
            List<OrderEntity> list = dao.findAllOrderList(customId);
            return list;
        }else {
            List<OrderEntity> orders = dao.findOrderWithOrderStateCustomId(customId,orderState);
            return orders;
        }
    }

    @Override
    public void saveOrder(OrderEntity entity) throws CommonException {
        entity.setOrder_id(SVUuidTool.getUUID());
        int rows = dao.saveOrder(entity);

        if (rows != 1){
            throw new CommonException(ExceptionEnum.Sql_insert_fail.getCode(),ExceptionEnum.Sql_insert_fail.getMessage());
        }
    }

    @Override
    public void updateOrderState(String orderId, String state) throws CommonException {

        if (orderId == null || orderId.length() == 0 || state == null || state.length() == 0){
            throw  new CommonException(ExceptionEnum.Request_Params_Invalid.getCode(),ExceptionEnum.Request_Params_Invalid.getMessage());
        }

        int rows = dao.updateOrderState(orderId,state);

        if (rows != 1){
            throw  new CommonException(ExceptionEnum.Sql_update_fail.getCode(),ExceptionEnum.Sql_update_fail.getMessage());
        }


    }








}
